﻿using Grow.Module.Options;
using Grow.Module.Services;
using Grow.Scheduler.CreateScheduler;
using Grow.Scheduler.Job;
using Microsoft.Extensions.DependencyInjection;

namespace Grow.Scheduler.Hangfire.Memory.CreateScheduler
{
    public class CreateSchedulerBuilder : ICreateSchedulerBuilder
    {

        private readonly IServiceContainer _serviceContainer;
        public CreateSchedulerBuilder(IServiceContainer serviceContainer)
        {
            _serviceContainer = serviceContainer;
        }

        public ICreateSchedulerBuilder AddJob<TJob>() where TJob : class, IJob
        {
            _serviceContainer.ServiceCollection.AddTransient<TJob>();
            return this;
        }

        public ICreateSchedulerBuilder AddJob<TJob, TJobData>() where TJob : class, IJob<TJobData>
        {
            _serviceContainer.ServiceCollection.AddTransient<TJob>();
            return this;
        }

        public ICreateSchedulerBuilder AddSchedulerOptions(Action<IOptionsBind, SchedulerOptions> schedulerOptionsAction)
        {
            _serviceContainer.OptionsManager.AddOptions<SchedulerOptions>(a =>
            {
                schedulerOptionsAction(_serviceContainer.OptionsManager.GetOptionsBind(), a);
            });
            return this;
        }


    }
}
